package com.chenzhou.dao;

import com.chenzhou.common.BaseDAO;
import com.chenzhou.common.DBuitl;
import com.chenzhou.entity.ProductEntity;
import com.chenzhou.entity.UserEntity;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDAOImpl extends BaseDAO<UserEntity>{

    @Override
    protected List<UserEntity> resultToList(ResultSet rs) throws SQLException {
        List<UserEntity> list = new ArrayList<>();
        while(rs.next()){
            UserEntity user = new UserEntity();
            user.setUid(rs.getInt("uid"));
            user.setUname(rs.getString("uname"));
            user.setNichen(rs.getString("nickname"));
            user.setPwd(rs.getString("pwd"));
            user.setPhone(rs.getString("phone"));
            user.setAddress(rs.getString("address"));
            user.setMarkup(rs.getString("markup"));
            user.setIsadmin(rs.getString("isadmin"));
            list.add(user);
        }
        return list;
    }

    /***
     * 根据用户名和密码判断用户是否存在
     * @param uname
     * @param pwd
     * @return
     */
    public UserEntity queryUserByUnameAndPassward(String uname,String pwd){
        String sql = "select * from tbl_user where uname = ? and pwd = ?";
        Object[] objects = new Object[]{uname,pwd};
        List<UserEntity> list = this.execute_Query(sql,objects);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
